// Fill out your copyright notice in the Description page of Project Settings.


#include "RunnerCharacterBase.h"

#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "Engine/LocalPlayer.h"
#include "EnhancedInputSubsystems.h"

ARunnerCharacterBase::ARunnerCharacterBase()
{
	PrimaryActorTick.bCanEverTick = true;
}

void ARunnerCharacterBase::BeginPlay()
{
	Super::BeginPlay();
}

void ARunnerCharacterBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	// 设置动作绑定
	if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
	{
		EnhancedInputComponent->BindAction(PowerLeftAction, ETriggerEvent::Started, this, &ThisClass::PowerLeftPressed);
		EnhancedInputComponent->BindAction(PowerLeftAction, ETriggerEvent::Completed, this, &ThisClass::PowerLeftReleased);

		EnhancedInputComponent->BindAction(PowerRigAction, ETriggerEvent::Started, this, &ThisClass::PowerRigPressed);
		EnhancedInputComponent->BindAction(PowerRigAction, ETriggerEvent::Completed, this, &ThisClass::PowerRigReleased);
	}
}

void ARunnerCharacterBase::PossessedBy(AController* NewController)
{
	Super::PossessedBy(NewController);
	if (MappingContext)
	{
		if (const APlayerController* PlayerController = Cast<APlayerController>(NewController))
		{
			if (UEnhancedInputLocalPlayerSubsystem* InputSubsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
			{
				InputSubsystem->AddMappingContext(MappingContext, 0);
			}
		}
	}
}
